package com.chixing.controller;

import com.chixing.pojo.Car;
import com.chixing.service.CarDetailService;
import com.chixing.service.RecommendService;
import com.chixing.util.ServerResult;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class CarDetailController {

    @Autowired
    private CarDetailService carDetailService;

    @Autowired
    private RecommendService recommendService;


    @GetMapping("/car/{carId}")
    public ModelAndView getById(@PathVariable("carId") Integer carId, @RequestParam(name="menberId",required = false) Integer menberId){
        System.out.println("carId" + carId);
        System.out.println("menberId" + menberId);
        ServerResult serverResult = carDetailService.getById(carId);
        List<Car> carList = null;
        if (menberId!=null) {
            System.out.println("用户推荐");
           carList = recommendService.getRecommentProductByUser(menberId, 3);
           if(carList.size()==0){
               System.out.println("用户推荐无效,进行默认推荐");
               carList = recommendService.getRecommentProductByUser(10001, 3);
           }
        } else{
           carList = recommendService.getRecommentProductByUser(10001, 3);
        }
        ModelAndView mav = new ModelAndView();
        System.out.println("运行后的数据"+serverResult.getCode());
        if (serverResult.getCode() == 200) {
            System.out.println("======serverResult.getData()======" + serverResult.getData());
            mav.addObject("pianhao",carList);
            mav.addObject("carDetailVo",serverResult.getData());
        } else {
            mav.addObject("carDetailVo",serverResult.getData());
        }
        mav.setViewName("car_rental/car_detail");
        System.out.println("返回页面");
        return mav;
    }


}
